# 智慧医疗RAG系统构建脚本
# 使用方法: .\build.ps1 [clean|build|test|run|docker]

param(
    [Parameter(Position=0)]
    [ValidateSet("clean", "build", "test", "run", "docker", "all")]
    [string]$Action = "all"
)

Write-Host "=== 智慧医疗RAG系统构建脚本 ===" -ForegroundColor Green
Write-Host "执行操作: $Action" -ForegroundColor Yellow

# 设置工作目录
$ProjectRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location $ProjectRoot

function Clean-Solution {
    Write-Host "`n1. 清理解决方案..." -ForegroundColor Yellow
    try {
        # 清理bin和obj目录
        Get-ChildItem -Path "src" -Recurse -Include "bin", "obj" | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
        Get-ChildItem -Path "tests" -Recurse -Include "bin", "obj" | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
        
        # 清理Docker镜像
        docker system prune -f
        
        Write-Host "清理完成" -ForegroundColor Green
    }
    catch {
        Write-Host "清理失败: $($_.Exception.Message)" -ForegroundColor Red
    }
}

function Build-Solution {
    Write-Host "`n2. 构建解决方案..." -ForegroundColor Yellow
    try {
        # 还原包
        dotnet restore
        
        # 构建所有项目
        dotnet build --configuration Release --no-restore
        
        Write-Host "构建完成" -ForegroundColor Green
    }
    catch {
        Write-Host "构建失败: $($_.Exception.Message)" -ForegroundColor Red
        exit 1
    }
}

function Test-Solution {
    Write-Host "`n3. 运行测试..." -ForegroundColor Yellow
    try {
        # 运行单元测试
        dotnet test --configuration Release --no-build --verbosity normal
        
        Write-Host "测试完成" -ForegroundColor Green
    }
    catch {
        Write-Host "测试失败: $($_.Exception.Message)" -ForegroundColor Red
        exit 1
    }
}

function Run-Application {
    Write-Host "`n4. 运行应用程序..." -ForegroundColor Yellow
    try {
        # 检查Ollama是否运行
        $ollamaStatus = Invoke-RestMethod -Uri "http://localhost:11434/api/tags" -Method Get -ErrorAction SilentlyContinue
        if (-not $ollamaStatus) {
            Write-Host "警告: Ollama服务未运行，请先启动Ollama" -ForegroundColor Yellow
            Write-Host "启动命令: ollama serve" -ForegroundColor Cyan
        }
        
        # 运行API
        Set-Location "src/SmartMedicalRAG.Api"
        dotnet run --configuration Release
        
        Write-Host "应用程序启动完成" -ForegroundColor Green
    }
    catch {
        Write-Host "运行失败: $($_.Exception.Message)" -ForegroundColor Red
        exit 1
    }
}

function Build-Docker {
    Write-Host "`n5. 构建Docker镜像..." -ForegroundColor Yellow
    try {
        # 构建Docker镜像
        docker-compose -f docker/docker-compose.yml build
        
        Write-Host "Docker镜像构建完成" -ForegroundColor Green
    }
    catch {
        Write-Host "Docker构建失败: $($_.Exception.Message)" -ForegroundColor Red
        exit 1
    }
}

# 执行操作
switch ($Action) {
    "clean" { Clean-Solution }
    "build" { Build-Solution }
    "test" { Test-Solution }
    "run" { Run-Application }
    "docker" { Build-Docker }
    "all" { 
        Clean-Solution
        Build-Solution
        Test-Solution
        Write-Host "`n所有操作完成!" -ForegroundColor Green
        Write-Host "要运行应用程序，请执行: .\build.ps1 run" -ForegroundColor Cyan
        Write-Host "要构建Docker镜像，请执行: .\build.ps1 docker" -ForegroundColor Cyan
    }
}

Write-Host "`n=== 构建脚本执行完成 ===" -ForegroundColor Green 